package com.lawyer.service;

import java.util.List;
import java.util.UUID;

import javax.persistence.EntityManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.lawyer.entity.Config;
import com.lawyer.entity.Lawyer;
import com.lawyer.entity.LawyerCaseInfo;
import com.lawyer.entity.LawyerMicroScoreInfo;
import com.lawyer.entity.User;
import com.lawyer.entity.UserLawyers;
import com.lawyer.model.Message;
import com.lawyer.repository.ConfigRepository;
import com.lawyer.repository.LawyerCaseInfoRepository;
import com.lawyer.repository.LawyerMicroScoreInfoRepository;
import com.lawyer.repository.LawyerRepository;
import com.lawyer.repository.UserLawyerRepository;
import com.lawyer.repository.UserRepository;

@Service
public class LawyerService {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private LawyerCaseInfoRepository lawyerCaseInfoRepository;

	@Autowired
	private LawyerRepository lawyerRepository;

	@Autowired
	private LawyerMicroScoreInfoRepository lawyerMicroScoreInfoRepository;

	@Autowired
	private EntityManager entityManager;
	@Autowired
	private UserRepository userRepository;
	@Autowired
	private UserLawyerRepository userLawyerRepository;

	@Autowired
	private ConfigRepository configRepository;

	public Message queryLawyerInfo(String lawyerId, String userId) {
		Message msg = new Message();

		Lawyer lawyer = lawyerRepository.findOne(lawyerId);
		lawyer.setPassword(null);

		String idStr = userId + lawyerId;
		String id = UUID.nameUUIDFromBytes(idStr.getBytes()).toString();
		UserLawyers userLawyer = userLawyerRepository.findOne(id);
		if (userLawyer != null) {
			// 已关注
			lawyer.setIsFocus(true);
		} else {
			// 没有关注
			lawyer.setIsFocus(false);
		}

		try {
			if (lawyer != null) {
				// 经典案例
				List<LawyerCaseInfo> list = lawyerCaseInfoRepository.findTop4ByLawyerIdOrderByStarLevelDesc(lawyerId);
				lawyer.setLawyerCaseInfos(list);
				// 连麦评分
				List<LawyerMicroScoreInfo> maiScoreLogs = lawyerMicroScoreInfoRepository.findTop4ByLawyerIdOrderByScoreDesc(lawyerId);
				lawyer.setMaiScoreLogs(maiScoreLogs);
				for (LawyerMicroScoreInfo scoreInfo : maiScoreLogs) {

					User user = userRepository.findOne(scoreInfo.getUserId());
					if (user != null) {
						scoreInfo.setNickName(user.getNickName());
						scoreInfo.setHeaderIco(user.getHeaderIco());
					}
				}
				Config config = configRepository.findByName("ConsultingFee");// 律师咨询费
				lawyer.setConsultPrice(Double.valueOf(config.getValue()));
				
				Config configPhone = configRepository.findByName("LawyerShowPhone");// 律师电话
				lawyer.setPhone(configPhone.getValue());


			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		msg.setData(lawyer);

		return msg;

	}

	public Message findByRecommend() {
		Message msg = new Message();
		List<Lawyer> lawyers = lawyerRepository.findByRecommendAndOnLineAndCertificationOrderByStarLevelDesc(true, "1", 2);
		for (Lawyer lawyer : lawyers) {
			if (lawyer != null) {

				// 经典案例
				List<LawyerCaseInfo> list = lawyerCaseInfoRepository.findTop4ByLawyerIdOrderByStarLevelDesc(lawyer.getId());
				lawyer.setLawyerCaseInfos(list);
				// 连麦评分
				List<LawyerMicroScoreInfo> maiScoreLogs = lawyerMicroScoreInfoRepository.findTop4ByLawyerIdOrderByScoreDesc(lawyer.getId());
				lawyer.setMaiScoreLogs(maiScoreLogs);

				Config config = configRepository.findByName("ConsultingFee");// 律师咨询费
				lawyer.setConsultPrice(Double.valueOf(config.getValue()));

			}
		}
		msg.setData(lawyers);
		return msg;

	}
}